package com.yic.module.device.service.inspectionPlanEntry;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.device.controller.admin.inspectionDeviceEntry.vo.InspectionDeviceEntryExportReqVO;
import com.yic.module.device.dal.dataobject.inspectionDeviceEntry.InspectionDeviceEntryDO;
import com.yic.module.device.service.inspectionDeviceEntry.InspectionDeviceEntryService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import com.yic.module.device.controller.admin.inspectionPlanEntry.vo.*;
import com.yic.module.device.dal.dataobject.inspectionPlanEntry.InspectionPlanEntryDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.device.convert.inspectionPlanEntry.InspectionPlanEntryConvert;
import com.yic.module.device.dal.mysql.inspectionPlanEntry.InspectionPlanEntryMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.device.enums.ErrorCodeConstants.*;

/**
 * 设备检查项 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class InspectionPlanEntryServiceImpl implements InspectionPlanEntryService {

    @Resource
    private InspectionPlanEntryMapper inspectionPlanEntryMapper;
    @Resource
    private InspectionDeviceEntryService inspectionDeviceEntryService;
    @Resource
    private TransactionTemplate transactionTemplate;

    @Override
    public Long createInspectionPlanEntry(InspectionPlanEntryCreateReqVO createReqVO) {
        // 插入
        InspectionPlanEntryDO inspectionPlanEntry = InspectionPlanEntryConvert.INSTANCE.convert(createReqVO);
        inspectionPlanEntryMapper.insert(inspectionPlanEntry);
        // 返回
        return inspectionPlanEntry.getId();
    }

    @Override
    public void updateInspectionPlanEntry(InspectionPlanEntryUpdateReqVO updateReqVO) {
        // 校验存在
        validateInspectionPlanEntryExists(updateReqVO.getId());
        // 更新
        InspectionPlanEntryDO updateObj = InspectionPlanEntryConvert.INSTANCE.convert(updateReqVO);
        inspectionPlanEntryMapper.updateById(updateObj);
    }

    @Override
    public void deleteInspectionPlanEntry(Long id,Boolean flag) {
        // 校验存在
        validateInspectionPlanEntryExists(id);
        transactionTemplate.execute(status -> {
            inspectionPlanEntryMapper.deleteById(id);
            // 删除设备和检查项关联信息
            if (ObjectUtil.isNotNull(flag) && flag.equals(Boolean.TRUE)) {
                InspectionDeviceEntryExportReqVO entryExportReqVO = new InspectionDeviceEntryExportReqVO();
                entryExportReqVO.setEntryId(id);
                List<InspectionDeviceEntryDO> inspectionDeviceEntryList = inspectionDeviceEntryService.getInspectionDeviceEntryList(entryExportReqVO);
                List<Long> idList = inspectionDeviceEntryList.stream().map(InspectionDeviceEntryDO::getId).collect(Collectors.toList());
                for (Long idT : idList) {
                    inspectionDeviceEntryService.deleteInspectionDeviceEntry(idT);
                }
            }
            return Boolean.TRUE;
        });
    }

    private void validateInspectionPlanEntryExists(Long id) {
        if (inspectionPlanEntryMapper.selectById(id) == null) {
            throw exception(INSPECTION_PLAN_ENTRY_NOT_EXISTS);
        }
    }

    @Override
    public InspectionPlanEntryDO getInspectionPlanEntry(Long id) {
        return inspectionPlanEntryMapper.selectById(id);
    }

    @Override
    public List<InspectionPlanEntryDO> getInspectionPlanEntryList(Collection<Long> ids) {
        return inspectionPlanEntryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InspectionPlanEntryDO> getInspectionPlanEntryPage(InspectionPlanEntryPageReqVO pageReqVO) {
        return inspectionPlanEntryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InspectionPlanEntryDO> getInspectionPlanEntryList(InspectionPlanEntryExportReqVO exportReqVO) {
        return inspectionPlanEntryMapper.selectList(exportReqVO);
    }

    @Override
    public PageResult<InspectionPlanEntryDO> getInspectionPlanEntryNotSelectPage(InspectionPlanEntryPageReqVO pageVO) {
        InspectionDeviceEntryExportReqVO deviceEntryExportReqVO = new InspectionDeviceEntryExportReqVO();
        deviceEntryExportReqVO.setDeviceId(pageVO.getDeviceId());
        List<InspectionDeviceEntryDO> inspectionDeviceEntryList = inspectionDeviceEntryService.getInspectionDeviceEntryList(deviceEntryExportReqVO);
        List<Long> entryIdList = inspectionDeviceEntryList.stream().map(InspectionDeviceEntryDO::getEntryId).collect(Collectors.toList());
        return inspectionPlanEntryMapper.selectNotSelectPage(pageVO,entryIdList);
    }

}
